package com.neu.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neu.constant.*;
import com.neu.mapper.ArticleMapper;
import com.neu.mapper.MaterialMapper;
import com.neu.mapper.ProjectMapper;
import com.neu.pojo.Article;
import com.neu.pojo.Plan;
import com.neu.pojo.Project;
import com.neu.service.ProjectService;
import com.neu.utils.ExcelListener;
import com.neu.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class ProjectServiceImpl implements ProjectService {

    @Resource
    ProjectMapper projectMapper;

    @Resource
    ArticleMapper articleMapper;

    @Resource
    MaterialMapper materialMapper;

    @Override
    public Project info(int id) {
        return projectMapper.queryProject(id);
    }

    @Override
    public PageInfo<Project> list(int currentPage, int pageSize) {
        PageInfo<Project> page = PageHelper.startPage(currentPage, pageSize).doSelectPageInfo(()-> projectMapper.queryProjectPage());
        return page;
    }

    @Override
    public int updateProject(Project project) {
        return projectMapper.updateProject(project);
    }

    @Override
    public int insertProject(Project project) {
        Article article = Article.builder().title(project.getName()+"-项目概况").content("").build();
        articleMapper.insertArticle(article);
        project.setSituation(article.getId());
        project.setProgress("");
        return projectMapper.insertProject(project);
    }

    @Override
    public int deleteProject(Project project) {
        articleMapper.deleteArticle(project.getSituation());
        materialMapper.deleteMaterialGroup(project.getId(), MaterialTypeEnum.PROJECT.type);
        materialMapper.deleteMaterialGroup(project.getSituation(), MaterialTypeEnum.ARTICLE.type);
        /*
            服务器文件待删除...
         */
        return projectMapper.deleteProject(project.getId());
    }

    @Override
    public Object batchInsertProject(MultipartFile file) throws IOException {
        ExcelReader excelReader = null;
        InputStream in = null;
        ExcelListener listener = new ExcelListener();
        try {
            in=file.getInputStream();
            excelReader = EasyExcel.read(in, Project.class,listener).build();
            excelReader.read(EasyExcel.readSheet(0).build());
            List<Project> projectList = new ArrayList<Project>();
            for(Object o:listener.getData()){
                Project project = (Project) o;
                if(project.getProvinceId()<=0||project.getProvinceId()>34){
                    return Result.error(ResultCodeEnum.EXCEL_PROJECT_PROVINCE_ERROR);
                }
                if(ProjectTypeEnum.of(project.getType())==ProjectTypeEnum.UNKNOWN){
                    return Result.error(ResultCodeEnum.EXCEL_PROJECT_TYPE_ERROR);
                }
                if(ProjectStatusEnum.of(project.getStatus())==ProjectStatusEnum.UNKNOWN){
                    return Result.error(ResultCodeEnum.EXCEL_PROJECT_STATUS_ERROR);
                }
                projectList.add(project);
            }
            for(Project project:projectList){
                Article article = Article.builder().title(project.getName()+"-项目概况").content("").build();
                articleMapper.insertArticle(article);
                project.setSituation(article.getId());
                project.setProgress("");
                projectMapper.insertProject(project);
            }
        } catch (Exception e) {
            log.error("导入失败",e);
            return Result.error(ResultCodeEnum.EXCEL_ERROR);
        } finally {
            in.close();
            // 这里一定别忘记关闭，读的时候会创建临时文件，到时磁盘会崩
            if (excelReader != null) {
                excelReader.finish();
            }
        }
        return 0;
    }
}
